package com.the4man.cnbdw.listener.socket;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Created by lenovo on 2017/12/12.
 */
public class SocketServiceLoader implements ServletContextListener {
    private UdpSocketThread udpsocketThread;
    private SocketThread socketThread;

    public SocketServiceLoader() {
    }

    public void contextDestroyed(ServletContextEvent arg0) {
        if(this.udpsocketThread != null && !this.udpsocketThread.isInterrupted()) {
            this.udpsocketThread.closeSocketServer();
            this.udpsocketThread.interrupt();
        }

    }

    public void contextInitialized(ServletContextEvent arg0) {
        WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(arg0.getServletContext());
        if(this.udpsocketThread == null) {
            this.udpsocketThread = new UdpSocketThread((UdpServerSocket)null, ctx);
            this.udpsocketThread.start();
        }

    }
}
